using Mathmall.Administration.Data;
using Mathmall.Administration.Models;
using Mathmall.Administration.Vo;
using Microsoft.EntityFrameworkCore;

namespace Mathmall.Administration.Mapper;

public class AttrGroupMapper : BaseMapper
{
    public AttrGroupMapper(AdministrationContext context) : base(context)
    {
    }

    /// <summary>
    /// 新增属性分组
    /// </summary>
    /// <param name="attrGroup"></param>
    /// <returns></returns>
    public async Task<int> CreateAttrGroup(AttrGroup attrGroup)
    {
        _context.Add(attrGroup);
        return await _context.SaveChangesAsync();
    }

    public IQueryable<AttrGroupVo> GetAttrGroupVos()
    {
        IQueryable<AttrGroupVo> attrGroupVos = from attrGroup in _context.AttrGroups
            join category in _context.Categories on attrGroup.CatalogID equals category.CategoryID
            select new AttrGroupVo
            {
                AttrGroupID = attrGroup.AttrGroupID, AttrGroupName = attrGroup.AttrGroupName, Sort = attrGroup.Sort,
                Description = attrGroup.Description, Icon = attrGroup.Icon, CatalogID = attrGroup.CatalogID,
                CatalogName = category.Name
            };
        return attrGroupVos;
    }

    /// <summary>
    /// 根据id查询属性分组
    /// </summary>
    /// <param name="attrgroupID"></param>
    /// <returns></returns>
    public async Task<AttrGroup> GetAttrGroupById(int attrgroupID)
    {
        return await _context.AttrGroups.Where(attrGroup => attrGroup.AttrGroupID == attrgroupID).AsNoTracking()
            .FirstAsync();
    }

    /// <summary>
    /// 修改属性分组
    /// </summary>
    /// <param name="attrGroup"></param>
    /// <returns></returns>
    public async Task<int> UpdateAttrGroup(AttrGroup attrGroup)
    {
        _context.Update(attrGroup);
        return await _context.SaveChangesAsync();
    }

    /// <summary>
    /// 删除属性分组
    /// </summary>
    /// <param name="attrGroupIDs"></param>
    /// <returns></returns>
    public async Task<int> DeleteAttrGroup(List<int> attrGroupIDs)
    {
        return await _context.AttrGroups.Where(attrGroup => attrGroupIDs.Contains(attrGroup.AttrGroupID))
            .ExecuteDeleteAsync();
    }

    /// <summary>
    /// 根据分类id查询属性分组
    /// </summary>
    /// <param name="catalogID"></param>
    /// <returns></returns>
    public async Task<List<AttrGroup>> GetAttrGroupsByCatalogId(int catalogID)
    {
        return await _context.AttrGroups.Where(attrGroup => attrGroup.CatalogID == catalogID).AsNoTracking()
            .ToListAsync();
    }

    /// <summary>
    /// 根据属性id查询属性分组信息
    /// </summary>
    /// <param name="attrID"></param>
    /// <returns></returns>
    public async Task<AttrGroup> GetAttrGroupByAttrId(int attrID)
    {
        AttrAttrgroupRelation attrAttrgroupRelation = await _context.AttrAttrgroupRelations
            .Where(attrAttrGroupRelation => attrAttrGroupRelation.AttrID == attrID).AsNoTracking().FirstAsync();
        return await _context.AttrGroups.Where(attrGroup => attrGroup.AttrGroupID == attrAttrgroupRelation.AttrGroupID)
            .FirstAsync();
    }
}